package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.Blog;
import com.hemaapp.yjnh.listener.OnItemClickListener;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类描述：粮库中的推荐商品
 * 创建人：GuiyouZhang
 * 创建时间：2016/8/1 16:26
 * 修改备注：
 */
public class LiangkuRecommodAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Blog> datas;
    private OnItemClickListener itemClickListener;

    public LiangkuRecommodAdapter(Context context, ArrayList<Blog> datas, OnItemClickListener itemClickListener) {
        super(context);
        this.mContext = context;
        this.datas = datas;
        this.itemClickListener = itemClickListener;
    }

    @Override
    public int getCount() {
        return datas.size() > 4 ? 4 : datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        ViewHolder holder;
        if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_liangku_recommod, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        } else
            holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
        holder.setDatas(position);
        return convertView;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_goods)
        ImageView ivGoods;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_cash)
        TextView tvCash;
        @Bind(R.id.tv_food_stamp)
        TextView tvFoodStamp;

        int position = -1;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null && position != -1)
                        itemClickListener.onClickListener(position);
                }
            });
        }

        public void setDatas(int position) {
            this.position = position;
            Blog blog = datas.get(position);
            tvName.setText(blog.getName());
            ImageUtils.loadSmalImage(mContext, blog.getImgurl(), ivGoods);
//            tvPrice.setText(blog.getPrice());
//            tvOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中间横线
//            tvOldPrice.setText("¥" + blog.getOldprice());

            double price = Double.parseDouble(blog.getPrice());
            double score = Double.parseDouble(blog.getScore());
            if (price - score < 0) {
                tvCash.setText("¥0.00");
            } else {
                tvCash.setText("¥" + uuUtils.formatAfterDot2(price - score + ""));
            }
            tvFoodStamp.setText("+");
            BaseUtil.addStampSign(mContext, tvFoodStamp, R.drawable.food_stamp_5);
            tvFoodStamp.append(blog.getScore());
        }
    }
}
